/*   Copyright 2012 Julian Aloofi

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#ifndef TACTICUS_SCREENELEMENT_HPP
#define TACTICUS_SCREENELEMENT_HPP

#include <utility>
#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Text.hpp>
#include <boost/shared_ptr.hpp>
#include "Common.hpp"

namespace Tacticus
{

namespace DisplayLayers
{

enum DisplayLayer { BACKGROUND, NORMAL, HIGHLIGHTED, FOREGROUND };

}

namespace ScreenElementTypes
{

enum ScreenElementType { BASE, SPRITE, TEXT };

}

typedef std::pair<DisplayLayers::DisplayLayer, boost::shared_ptr<sf::Drawable> >
    LayeredDrawable;

class ScreenElement
{
public:
    ScreenElement() {}
    virtual ~ScreenElement() {}
    virtual ScreenElementTypes::ScreenElementType getType() const
        { return ScreenElementTypes::BASE; }
    const LayeredDrawable &getLayeredDrawable() const
        { return layeredDrawable; }

    void setPosition(float x, float y)
    {
        if (getType() == ScreenElementTypes::SPRITE)
        {
            boost::shared_ptr<sf::Sprite> elemPtr = boost::static_pointer_cast<
                sf::Sprite>(layeredDrawable.second);
            elemPtr->setPosition(x, y);
        }
        else
        {
            boost::shared_ptr<sf::Text> elemPtr = boost::static_pointer_cast<
                sf::Text>(layeredDrawable.second);
            elemPtr->setPosition(x, y);
        }
    }

    void setPosition(Common::ScreenCoordinate position)
        { setPosition(position.x, position.y); }

    sf::FloatRect const getBounds() const
    {
        if (getType() == ScreenElementTypes::SPRITE)
        {
            boost::shared_ptr<sf::Sprite> elemPtr = boost::static_pointer_cast<
                sf::Sprite>(layeredDrawable.second);
            return elemPtr->getGlobalBounds();
        }
        else
        {
            boost::shared_ptr<sf::Text> elemPtr = boost::static_pointer_cast<
                sf::Text>(layeredDrawable.second);
            return elemPtr->getGlobalBounds();
        }
    }

    bool operator<(const ScreenElement &rhs) const
        { return (layeredDrawable.first < rhs.getLayeredDrawable().first); }
protected:
    LayeredDrawable layeredDrawable;
};

class SpriteElement : public ScreenElement
{
public:
    SpriteElement(DisplayLayers::DisplayLayer desiredLayer,
        boost::shared_ptr<sf::Sprite> sprite)
        { layeredDrawable = LayeredDrawable(desiredLayer, sprite); }
    ScreenElementTypes::ScreenElementType getType() const
        { return ScreenElementTypes::SPRITE; }
};

class TextElement : public ScreenElement
{
public:
    TextElement(DisplayLayers::DisplayLayer desiredLayer,
        boost::shared_ptr<sf::Text> text)
        { layeredDrawable = LayeredDrawable(desiredLayer, text); }
    ScreenElementTypes::ScreenElementType getType() const
        { return ScreenElementTypes::TEXT; }
};

}

#endif
